package com.lgqz.himalayafm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lgqz.himalayafm.R;
import com.lgqz.himalayafm.bean.TrackListItem;
import com.lgqz.himalayafm.util.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.List;

/**
 * Description:
 * Copyright(c) 2016,邱国兴
 * This Program is protected by copyright laws
 * Created by Little Monster on 2016/12/13.
 *
 * @author 邱国兴
 */

public class ProgramAdapter extends RecyclerView.Adapter<ProgramAdapter.ViewHolder> {
    private Context context;
    private List<TrackListItem> trackList;

    public ProgramAdapter(Context context, List<TrackListItem> trackList) {
        this.context = context;
        this.trackList = trackList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_program, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        TrackListItem listItem = trackList.get(position);
        holder.mIcon.setImageURI(listItem.getSmallLogo());
        holder.mTitle.setText(listItem.getTitle());
        holder.mCount.setText(listItem.getPlaytimes() + "");
        holder.mComment.setText(listItem.getComments() + "");

        long createdAt = listItem.getCreatedAt();
        String dateDiff = DateUtils.getDateDiff(createdAt);
        holder.mTime.setText(dateDiff);

        int duration = listItem.getDuration();
        SimpleDateFormat sdf = new SimpleDateFormat("MM:ss");
        String format = sdf.format(duration);
        holder.mLength.setText(format);
    }

    @Override
    public int getItemCount() {
        return trackList.size();
    }

    public void addAll(Collection<? extends TrackListItem> collection) {
        int size = trackList.size();
        trackList.addAll(collection);
        notifyItemRangeInserted(size, collection.size());
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private SimpleDraweeView mIcon;
        private TextView mTitle;
        private TextView mTime;
        private TextView mCount;
        private TextView mLength;
        private TextView mComment;

        ViewHolder(View itemView) {
            super(itemView);
            mIcon = ((SimpleDraweeView) itemView.findViewById(R.id.sdv_icon_program_item));
            mTitle = ((TextView) itemView.findViewById(R.id.tv_title_program_item));
            mTime = ((TextView) itemView.findViewById(R.id.tv_time_program_item));
            mCount = ((TextView) itemView.findViewById(R.id.tv_count_program_item));
            mLength = ((TextView) itemView.findViewById(R.id.tv_length_program_item));
            mComment = ((TextView) itemView.findViewById(R.id.tv_comment_program_item));
        }
    }
}
